package cn.itcast.controller;

import cn.itcast.pojo.PageResult;
import cn.itcast.pojo.Result;
import cn.itcast.pojo.Student;
import cn.itcast.pojo.StudentQueryParam;
import cn.itcast.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.websocket.servlet.TomcatWebSocketServletWebServerCustomizer;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TomcatWebSocketServletWebServerCustomizer tomcatWebSocketServletWebServerCustomizer;

    /*分页查询*/
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam) {
        PageResult<Student> pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }

    /*添加学员*/
    @PostMapping
    public Result save(@RequestBody Student student){
        studentService.save(student);
        return Result.success();
    }
    /*根据id查询*/
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    /*修改根据*/

    @PutMapping
    public Result update(@RequestBody Student student){
        studentService.update(student);
        return Result.success();
    }

    /*批量删除*/
    @DeleteMapping("/{ids}")
    public Result deleteBatch(@PathVariable Integer[] ids){
        log.info("-----------------------------------------------------------{}", Arrays.toString(ids));
        studentService.deleteBatch(ids);
        return Result.success();
    }
    //违纪管理
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id ,@PathVariable Integer score){
        log.info("违纪id={},扣分{}",id,score);
        studentService.violation(id,score);
        return Result.success();
    }
}
